package Reflex.method;

import Reflex.field.Cat;
import com.sun.source.tree.MethodTree;
import org.junit.Test;

import java.lang.reflect.Method;

public class TestMethods {
    @Test
    public void testGetMenthods() throws Exception {
        Class catClass=Cat2.class;
        Method[] methods=catClass.getDeclaredMethods();
        for (Method method : methods) {
            System.out.println(method.getName()+method.getParameterCount());
        }
        Method method=catClass.getDeclaredMethod("run");//拿无参的run方法
        System.out.println(method.getName()+
                method.getParameterCount()+
                method.getReturnType());

    }

    @Test
    public void testGetMenthods02() throws Exception {
        //获得方法对象的作用是执行
        Cat2 cat2=new Cat2();
        Class catClass = Cat2.class;
        Method method = catClass.getDeclaredMethod("eat");//cat2类里面叫做eat的方法
        method.setAccessible(true);//私有原因不可直接访问
        Object rs=method.invoke(cat2);//调用cat2对象的eat方法，调用无参数的run方法,返回值void
        System.out.println(rs);
    }
}
